import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";

import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import path from "path";
// element-plus
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// element-plus/icons-vue
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";

export default defineConfig({
  plugins: [
    AutoImport({
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: ["vue"],
      // imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
      resolvers: [
        // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
        ElementPlusResolver(),
        // 自动导入图标组件
        IconsResolver({
          // prefix: "Icon",
        }),
      ],
      vueTemplate: true,
      // 配置文件生成位置(false:关闭自动生成)
      // dts: false,
      dts: "src/types/auto-imports.d.ts", // 指定自动导入函数TS类型声明文件路径
      // dts: path.resolve(path.resolve(__dirname, "src"), "auto-imports.d.ts"),
    }),
    Components({
      resolvers: [
        // 自动导入 Element Plus 组件
        ElementPlusResolver(),
        // 自动注册图标组件
        IconsResolver({
          // @iconify-json/ep 是 Element Plus 的图标库
          enabledCollections: ["ep"],
        }),
      ],
      // 指定自定义组件位置(默认:src/components)
      dirs: ["src/**/components", "src/layout"],
      // 配置文件位置(false:关闭自动生成)
      // dts: false,
      dts: "src/types/components.d.ts",
      // dts: path.resolve(path.resolve(__dirname, "src"), "components.d.ts"),
    }),
    Icons({
      autoInstall: true,
    }),
    createSvgIconsPlugin({
      // Specify the icon folder to be cached
      iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
      // Specify symbolId format
      symbolId: "icon-[dir]-[name]",
    }),
    vue(),
  ],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
      "@api": path.resolve(__dirname, "src/api"),
      "@components": path.resolve(__dirname, "src/components"),
      "@assets": path.resolve(__dirname, "src/assets"),
      "@store": path.resolve(__dirname, "src/store"),
      "@layout": path.resolve(__dirname, "src/layout"),
      "@router": path.resolve(__dirname, "src/router"),
      "@utils": path.resolve(__dirname, "src/utils"),
      "@views": path.resolve(__dirname, "src/views"),
      "@types": path.resolve(__dirname, "src/types"),
    },
  },
  //scss全局变量一个配置
  css: {
    preprocessorOptions: {
      scss: {
        api: "modern-compiler",
        silenceDeprecations: ["legacy-js-api"],
        additionalData: `@use  "@/assets/styles/variables.scss" as *;`,
      },
    },
  },
  // 配置跨域请求的代理服务器
  server: {
    proxy: {
      "/kbs-backend": {
        target: "http://localhost:8085/kbs-backend/",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/kbs-backend/, ""),
      },
    },
  },
});
